﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package org.aswing.resizer {
    import flash.events.*;
    import flash.display.*;
    import org.aswing.*;
    import org.aswing.geom.*;
    import org.aswing.plaf.*;
    import org.aswing.event.*;
    import org.aswing.graphics.*;
    import org.aswing.util.*;

    public class DefaultResizer implements Resizer, UIResource {

        protected static var RESIZE_MC_WIDTH:Number = 4;

        protected var owner:Component;
        protected var resizeMC:Sprite;
        protected var resizeArrowCursor:DisplayObject;
        protected var boundsShape:Shape;
        protected var topResizeMC:AWSprite;
        protected var leftResizeMC:AWSprite;
        protected var bottomResizeMC:AWSprite;
        protected var rightResizeMC:AWSprite;
        protected var topLeftResizeMC:AWSprite;
        protected var topRightResizeMC:AWSprite;
        protected var bottomLeftResizeMC:AWSprite;
        protected var bottomRightResizeMC:AWSprite;
        private var startX:Number;
        private var startY:Number;
        private var startBounds:IntRectangle;
        protected var enabled:Boolean;
        protected var resizeDirectly:Boolean;
        protected var resizeArrowColor:ASColor;
        protected var resizeArrowLightColor:ASColor;
        protected var resizeArrowDarkColor:ASColor;
        private var resizingNow:Boolean = false;
        private var lastRepresentedBounds:IntRectangle;

        public function DefaultResizer(){
            this.enabled = true;
            this.resizeDirectly = false;
            this.startX = 0;
            this.startY = 0;
            this.startBounds = new IntRectangle();
            this.resizeArrowColor = UIManager.getColor("resizeArrow");
            this.resizeArrowLightColor = UIManager.getColor("resizeArrowLight");
            this.resizeArrowDarkColor = UIManager.getColor("resizeArrowDark");
        }
        public function setResizeArrowColor(_arg1:ASColor):void{
            this.resizeArrowColor = _arg1;
        }
        public function setResizeArrowLightColor(_arg1:ASColor):void{
            this.resizeArrowLightColor = _arg1;
        }
        public function setResizeArrowDarkColor(_arg1:ASColor):void{
            this.resizeArrowDarkColor = _arg1;
        }
        public function setOwner(_arg1:Component):void{
            if (this.owner != null){
                this.owner.removeEventListener(AWEvent.PAINT, this.locate);
                if (this.resizeMC != null){
                    this.owner.removeChild(this.resizeMC);
                };
                this.hideBoundsMC();
            };
            this.owner = _arg1;
            if (this.owner != null){
                this.owner.addEventListener(AWEvent.PAINT, this.locate);
                if (this.resizeMC == null){
                    this.createResizeMCs();
                };
                this.owner.addChildAt(this.resizeMC, this.owner.numChildren);
            };
            this.locate();
        }
        public function setResizeDirectly(_arg1:Boolean):void{
            this.resizeDirectly = _arg1;
        }
        public function isResizeDirectly():Boolean{
            return (this.resizeDirectly);
        }
        public function setArrowRotation(_arg1:Number):void{
            this.resizeArrowCursor.rotation = _arg1;
        }
        public function startArrowCursor():void{
            if (((this.resizeMC) && (this.resizeMC.stage))){
                CursorManager.getManager(this.resizeMC.stage).showCustomCursor(this.resizeArrowCursor);
            };
        }
        public function stopArrowCursor():void{
            if (((this.resizeMC) && (this.resizeMC.stage))){
                CursorManager.getManager(this.resizeMC.stage).hideCustomCursor(this.resizeArrowCursor);
            };
        }
        public function setResizing(_arg1:Boolean):void{
            this.resizingNow = _arg1;
        }
        public function isResizing():Boolean{
            return (this.resizingNow);
        }
        public function startResize(_arg1:ResizeStrategy, _arg2:MouseEvent):void{
            if (!this.resizeDirectly){
                this.representRect(this.owner.getComBounds());
            };
            this.startX = _arg2.stageX;
            this.startY = _arg2.stageY;
            this.startBounds = this.owner.getComBounds();
        }
        public function resizing(_arg1:ResizeStrategy, _arg2:MouseEvent):void{
            var _local3:IntRectangle = _arg1.getBounds(this.startBounds, this.owner.getMinimumSize(), this.owner.getMaximumSize(), (_arg2.stageX - this.startX), (_arg2.stageY - this.startY));
            if (this.resizeDirectly){
                this.owner.setBounds(_local3);
                this.owner.revalidate();
                _arg2.updateAfterEvent();
            } else {
                this.representRect(_local3);
            };
        }
        public function finishResize(_arg1:ResizeStrategy):void{
            if (!this.resizeDirectly){
                this.owner.setComBounds(this.lastRepresentedBounds);
                this.hideBoundsMC();
                this.owner.revalidate();
            };
        }
        private function hideBoundsMC():void{
            var _local1:DisplayObjectContainer = this.owner.parent;
            if (((((!((this.boundsShape == null))) && (!((_local1 == null))))) && (_local1.contains(this.boundsShape)))){
                _local1.removeChild(this.boundsShape);
            };
        }
        private function representRect(_arg1:IntRectangle):void{
            var _local2:DisplayObjectContainer;
            var _local3:Insets;
            var _local4:IntRectangle;
            var _local5:Number;
            var _local6:Number;
            var _local7:Number;
            var _local8:Number;
            var _local9:Graphics2D;
            if (!this.resizeDirectly){
                _local2 = this.owner.parent;
                if (!_local2.contains(this.boundsShape)){
                    _local2.addChild(this.boundsShape);
                };
                DepthManager.bringToTop(this.boundsShape);
                _local3 = this.owner.getResizerMargin();
                _local4 = _arg1.clone();
                _arg1.clone().x = (_local4.x + _local3.left);
                _local4.y = (_local4.y + _local3.top);
                _local4.width = (_local4.width - _local3.getMarginWidth());
                _local4.height = (_local4.height - _local3.getMarginHeight());
                _local5 = _local4.x;
                _local6 = _local4.y;
                _local7 = _local4.width;
                _local8 = _local4.height;
                _local9 = new Graphics2D(this.boundsShape.graphics);
                this.boundsShape.graphics.clear();
                _local9.drawRectangle(new Pen(this.resizeArrowLightColor), (_local5 - 1), (_local6 - 1), (_local7 + 2), (_local8 + 2));
                _local9.drawRectangle(new Pen(this.resizeArrowColor), _local5, _local6, _local7, _local8);
                _local9.drawRectangle(new Pen(this.resizeArrowDarkColor), (_local5 + 1), (_local6 + 1), (_local7 - 2), (_local8 - 2));
                this.lastRepresentedBounds = _arg1;
            };
        }
        protected function createResizeMCs():void{
            var _local1:Number = RESIZE_MC_WIDTH;
            this.resizeMC = new Sprite();
            this.resizeMC.name = "resizer";
            this.resizeArrowCursor = Cursor.createCursor(Cursor.H_RESIZE_CURSOR);
            this.resizeArrowCursor.name = "resizeCursor";
            this.boundsShape = new Shape();
            this.boundsShape.name = "bounds";
            this.topResizeMC = new AWSprite();
            this.leftResizeMC = new AWSprite();
            this.rightResizeMC = new AWSprite();
            this.bottomResizeMC = new AWSprite();
            this.topLeftResizeMC = new AWSprite();
            this.topRightResizeMC = new AWSprite();
            this.bottomLeftResizeMC = new AWSprite();
            this.bottomRightResizeMC = new AWSprite();
            this.resizeMC.addChild(this.topResizeMC);
            this.resizeMC.addChild(this.leftResizeMC);
            this.resizeMC.addChild(this.rightResizeMC);
            this.resizeMC.addChild(this.bottomResizeMC);
            this.resizeMC.addChild(this.topLeftResizeMC);
            this.resizeMC.addChild(this.topRightResizeMC);
            this.resizeMC.addChild(this.bottomLeftResizeMC);
            this.resizeMC.addChild(this.bottomRightResizeMC);
            DefaultResizeBarHandler.createHandler(this, this.topResizeMC, 90, this.createResizeStrategy(0, -1));
            DefaultResizeBarHandler.createHandler(this, this.leftResizeMC, 0, this.createResizeStrategy(-1, 0));
            DefaultResizeBarHandler.createHandler(this, this.rightResizeMC, 0, this.createResizeStrategy(1, 0));
            DefaultResizeBarHandler.createHandler(this, this.bottomResizeMC, 90, this.createResizeStrategy(0, 1));
            DefaultResizeBarHandler.createHandler(this, this.topLeftResizeMC, 45, this.createResizeStrategy(-1, -1));
            DefaultResizeBarHandler.createHandler(this, this.topRightResizeMC, -45, this.createResizeStrategy(1, -1));
            DefaultResizeBarHandler.createHandler(this, this.bottomLeftResizeMC, -45, this.createResizeStrategy(-1, 1));
            DefaultResizeBarHandler.createHandler(this, this.bottomRightResizeMC, 45, this.createResizeStrategy(1, 1));
            var _local2:SolidBrush = new SolidBrush(new ASColor(0, 0));
            var _local3:Graphics2D = new Graphics2D(this.topResizeMC.graphics);
            _local3.fillRectangle(_local2, 0, 0, _local1, _local1);
            _local3 = new Graphics2D(this.leftResizeMC.graphics);
            _local3.fillRectangle(_local2, 0, 0, _local1, _local1);
            _local3 = new Graphics2D(this.rightResizeMC.graphics);
            _local3.fillRectangle(_local2, -(_local1), 0, _local1, _local1);
            _local3 = new Graphics2D(this.bottomResizeMC.graphics);
            _local3.fillRectangle(_local2, 0, -(_local1), _local1, _local1);
            _local3 = new Graphics2D(this.topLeftResizeMC.graphics);
            _local3.fillRectangle(_local2, 0, 0, (_local1 * 2), _local1);
            _local3.fillRectangle(_local2, 0, 0, _local1, (_local1 * 2));
            _local3 = new Graphics2D(this.topRightResizeMC.graphics);
            _local3.fillRectangle(_local2, (-(_local1) * 2), 0, (_local1 * 2), _local1);
            _local3.fillRectangle(_local2, -(_local1), 0, _local1, (_local1 * 2));
            _local3 = new Graphics2D(this.bottomLeftResizeMC.graphics);
            _local3.fillRectangle(_local2, 0, -(_local1), (_local1 * 2), _local1);
            _local3.fillRectangle(_local2, 0, (-(_local1) * 2), _local1, (_local1 * 2));
            _local3 = new Graphics2D(this.bottomRightResizeMC.graphics);
            _local3.fillRectangle(_local2, (-(_local1) * 2), -(_local1), (_local1 * 2), _local1);
            _local3.fillRectangle(_local2, -(_local1), (-(_local1) * 2), _local1, (_local1 * 2));
            this.resizeMC.visible = this.enabled;
        }
        private function createResizeStrategy(_arg1:Number, _arg2:Number):ResizeStrategy{
            return (new ResizeStrategyImp(_arg1, _arg2));
        }
        public function setEnabled(_arg1:Boolean):void{
            this.enabled = _arg1;
            this.resizeMC.visible = this.enabled;
        }
        public function isEnabled():Boolean{
            return (this.enabled);
        }
        private function locate(_arg1:Event=null):void{
            if (this.owner == null){
                return;
            };
            if (this.owner.getChildIndex(this.resizeMC) < (this.owner.getHighestIndexUnderForeground() - 1)){
                this.owner.bringToTop(this.resizeMC);
            };
            var _local2:Insets = this.owner.getResizerMargin();
            var _local3:Number = _local2.left;
            var _local4:Number = _local2.top;
            var _local5:Number = ((this.owner.getWidth() - _local2.right) - _local2.left);
            var _local6:Number = ((this.owner.getHeight() - _local2.bottom) - _local2.top);
            var _local7:Number = RESIZE_MC_WIDTH;
            this.topResizeMC.width = Math.max(0, (_local5 - (_local7 * 2)));
            this.topResizeMC.x = _local7;
            this.topResizeMC.y = _local4;
            this.leftResizeMC.height = Math.max(0, (_local6 - (_local7 * 2)));
            this.leftResizeMC.x = _local3;
            this.leftResizeMC.y = _local7;
            this.rightResizeMC.height = Math.max(0, (_local6 - (_local7 * 2)));
            this.rightResizeMC.x = (_local3 + _local5);
            this.rightResizeMC.y = _local7;
            this.bottomResizeMC.width = Math.max(0, (_local5 - (_local7 * 2)));
            this.bottomResizeMC.x = _local7;
            this.bottomResizeMC.y = (_local4 + _local6);
            this.topLeftResizeMC.x = _local3;
            this.topLeftResizeMC.y = _local4;
            this.topRightResizeMC.x = (_local3 + _local5);
            this.topRightResizeMC.y = _local4;
            this.bottomLeftResizeMC.x = _local3;
            this.bottomLeftResizeMC.y = (_local4 + _local6);
            this.bottomRightResizeMC.x = (_local3 + _local5);
            this.bottomRightResizeMC.y = (_local4 + _local6);
        }

    }
}//package org.aswing.resizer 
